package com.milk.milkmedia.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.milk.milkmedia.R;
import com.milk.milkmedia.api.BaseActivity;

public class LoginActivity extends BaseActivity {

	final static String LOGIN_XML = "haha.xml";
	final static String PASSWORD = "psd";
	final static String IS_SAVE_PSD = "isSavePsd";
	final static String NAME = "name";
	final static String IS_AUTO_LOGIN = "isAutoLogin";
	private Button mLoginButton;
	private TextView mRegister;
	private CheckBox mAutoPassword;
	private CheckBox mAutoLogin;
	private EditText etUser;
	private EditText etPassword;
	private String userValue, passwordValue;
	Intent intent = new Intent();
	SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		sp = this.getSharedPreferences(LOGIN_XML, Activity.MODE_PRIVATE);

		mLoginButton = (Button) findViewById(R.id.login_button);
		mRegister = (TextView) findViewById(R.id.registerView);
		mAutoPassword = (CheckBox) findViewById(R.id.autoPassword);
		mAutoLogin = (CheckBox) findViewById(R.id.autoLogin);
		etUser = (EditText) findViewById(R.id.etUser);
		etPassword = (EditText) findViewById(R.id.etPassword);

		if (sp.getBoolean(IS_SAVE_PSD, false)) {
			mAutoPassword.setChecked(true);
			etUser.setText(sp.getString(NAME, null));
			etPassword.setText(sp.getString(PASSWORD, null));
			if (sp.getBoolean(IS_AUTO_LOGIN, false)) {
				mAutoLogin.setChecked(true);
				intent.setClass(LoginActivity.this, SecondActivity.class);
				startActivity(intent);
			}
		}

		// 记住密码
		mAutoPassword.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Editor editor = sp.edit();
				if (mAutoPassword.isChecked()) {

					System.out.println("记住密码选中");
					editor.putBoolean(IS_SAVE_PSD, true).commit();
				} else {
					System.out.println("记住密码未选中");
					editor.putBoolean(IS_SAVE_PSD, false).commit();
				}
			}

		});

		// 自动登录
		mAutoLogin.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Editor editor = sp.edit();
				if (mAutoLogin.isChecked()) {

					System.out.println("自动登录选中");
					editor.putBoolean(IS_AUTO_LOGIN, true).commit();
				} else {
					System.out.println("自动登录未选中");
					editor.putBoolean(IS_AUTO_LOGIN, false).commit();
				}
			}

		});

		// 登录按钮123 123
		mLoginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				userValue = etUser.getText().toString();
				passwordValue = etPassword.getText().toString();

				if (userValue.equals("123") && passwordValue.equals("123")) {
					Toast.makeText(LoginActivity.this, "登录成功",
							Toast.LENGTH_LONG).show();
					System.out.println("登录成功");
					if (mAutoPassword.isChecked()) {
						Editor editor = sp.edit();
						editor.putString(NAME, userValue);
						editor.putString(PASSWORD, passwordValue);
						editor.commit();
					}
					intent.setClass(LoginActivity.this, SecondActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(LoginActivity.this, "用户名或密码错误",
							Toast.LENGTH_LONG).show();
					System.out.println("用户名或密码错误");
				}

			}
		});

		// 注册
		mRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				intent.setClass(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
			}
		});

	}

}
